package io.renren.modules.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.edu.dao.TEduTrainDao;
import io.renren.modules.edu.dto.TEduTrainDTO;
import io.renren.modules.edu.entity.TEduScientificEntity;
import io.renren.modules.edu.entity.TEduTrainEntity;
import io.renren.modules.edu.service.TEduTeacherinfoService;
import io.renren.modules.edu.service.TEduTrainService;
import io.renren.modules.edu.service.TEduTrainTeacherService;
import io.renren.modules.edu.vo.TEduScientificVO;
import io.renren.modules.edu.vo.TEduTeacherinfoVO;
import io.renren.modules.edu.vo.TEduTrainTeacherVO;
import io.renren.modules.edu.vo.TEduTrainVO;
import io.renren.modules.security.SecurityUser;
import io.renren.modules.security.UserDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 培训管理表
 *
 * @author zwp zwp
 * @since 1.0.0 2022-07-11
 */
@Slf4j
@Service
public class TEduTrainServiceImpl extends ServiceImpl<TEduTrainDao, TEduTrainEntity> implements TEduTrainService {

    @Autowired
    private TEduTeacherinfoService tEduTeacherinfoService;

    @Autowired
    private TEduTrainTeacherService tEduTrainTeacherService;

    @Override
    public void save(TEduTrainDTO tEduTrainDTO) {
        TEduTrainEntity tEduTrainEntity = JacksonUtils.obj2pojo(tEduTrainDTO, TEduTrainEntity.class);
        this.baseMapper.insert(tEduTrainEntity);
    }

    @Override
    public void update(TEduTrainDTO tEduTrainDTO) {
        TEduTrainEntity tEduTrainEntity = JacksonUtils.obj2pojo(tEduTrainDTO, TEduTrainEntity.class);
        this.updateById(tEduTrainEntity);
    }

    @Override
    public PageData<TEduTrainVO> queryPageData(Map<String, Object> params) {
        log.info("培训管理信息分页");
        // 获取当前用户
        UserDetail user = SecurityUser.getUser();
        // 获取请求对象(用于分页筛选条件)
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        String trainingName = (String) params.get("trainingName");
        // 获取起始长度
        Integer size = Integer.valueOf(limit);
        Integer index = Integer.valueOf(page);
        // 获取教师信息
        TEduTeacherinfoVO tEduTeacherinfoVO = tEduTeacherinfoService.queryTeacherBySysUserId(user.getId());
        // 查询该教师报名情况
        Map<Long, List<TEduTrainTeacherVO>> trainToTeacher = tEduTrainTeacherService.queryTrainToTeacher(tEduTeacherinfoVO.getId());
        // 查询培训管理表信息
        IPage<TEduTrainEntity> iPage = new Page<>(index, size);
        QueryWrapper<TEduTrainEntity> wrapper = new QueryWrapper<TEduTrainEntity>();
        if (StringUtils.isNotBlank(trainingName)) {
            wrapper.like("training_name", trainingName);
        }
        wrapper.orderByDesc("create_date");
        this.baseMapper.selectPage(iPage, wrapper);

        List<TEduTrainVO> tEduTrainVOList = JacksonUtils.listObj2pojo(iPage.getRecords(), TEduTrainVO.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tEduTrainVOList.forEach(e -> {
            if (trainToTeacher != null) {
                List<TEduTrainTeacherVO> tEduTrainTeacherVOList = trainToTeacher.get(e.getId());
                if (CollectionUtils.isNotEmpty(tEduTrainTeacherVOList)) {
                    // 已报名
                    e.setStatus(1);
                } else {
                    // 未报名
                    e.setStatus(0);
                }
            } else {
                e.setStatus(0);
            }
            e.setTrainingStartTime(simpleDateFormat.format(new Date(e.getTrainingStartDate())));
            e.setTrainingEndTime(simpleDateFormat.format(new Date(e.getTrainingEndDate())));
        });
        return new PageData<TEduTrainVO>(tEduTrainVOList, iPage.getTotal());
    }

    @Override
    public void confirmTrain(Long id) {
        log.info("参与报名");
        // 获取当前时间
        Long nowDate = System.currentTimeMillis();
        // 获取当前用户
        UserDetail user = SecurityUser.getUser();
        TEduTrainEntity tEduTrainEntity = this.baseMapper.selectById(id);
        // 培训结束时间
        Long trainingEndDate = tEduTrainEntity.getTrainingEndDate();
        if (nowDate.compareTo(trainingEndDate) > 0) {
            throw new RRException("该培训活动时间已经截止！无法报名！");
        }
        // 获取教师信息
        TEduTeacherinfoVO tEduTeacherinfoVO = tEduTeacherinfoService.queryTeacherBySysUserId(user.getId());
        if (tEduTeacherinfoVO == null) {
            throw new RRException("未录入教师信息！报名无效！");
        }
        tEduTrainTeacherService.confirmTrain(id, tEduTeacherinfoVO.getId());
    }

    @Override
    public TEduTrainVO queryTrainById(Long id) {
        log.info("根据id获取培训管理信息");
        TEduTrainEntity tEduTrainEntity = this.baseMapper.selectById(id);
        return JacksonUtils.obj2pojo(tEduTrainEntity, TEduTrainVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTrain(Long id) {
        log.info("培训活动删除");
        this.baseMapper.deleteById(id);
        tEduTrainTeacherService.deleteTrainTeaByTrainId(id);
    }
}
